package com.ruoyi.system.service.impl;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONConfig;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.system.domain.ResultSave;
import com.ruoyi.system.mapper.SystemResultSaveMapper;
import com.ruoyi.system.service.IResultSaveService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 参数和文件保存表 服务实现类
 * </p>
 *
 * @author liuhl
 * @since 2024-06-25
 */
@Service
public class SystemResultSaveServiceImpl extends ServiceImpl<SystemResultSaveMapper, ResultSave> implements IResultSaveService {

    //排查 某卫星 某时间是否融合
    public void ini() {

        List<Integer> indexs = ListUtil.of(1,4,7,10,13,16,19,22);
        DateTime start = DateUtil.parse("2014-03-01");
        DateTime end = DateUtil.parse("2015-03-01");
        DateTime now = null;
        HashSet<String> timeSet = new HashSet<>();
        for (int i = 0; i < 365 ; i++) {
            now = DateUtil.offsetDay(start,i);
            for (Integer index : indexs) {
                DateTime dateTime = now.setField(DateField.HOUR_OF_DAY, index);
                String format = DateUtil.format(dateTime, "yyyy-MM-dd:H");
                timeSet.add(format);
            }
        }


        LambdaQueryWrapper<ResultSave> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ResultSave::getSubmodule,"DATA_FUSION_GRID");
        queryWrapper.select(ResultSave::getParams,ResultSave::getStartTime);
        queryWrapper.between(ResultSave::getStartTime,"2014-03-01","2015-03-01");
        queryWrapper.orderByAsc(ResultSave::getStartTime);
        List<ResultSave> list = this.list(queryWrapper);
         List<ResultSave> resultSaves = list.stream()
                .filter(a -> {return a.getParams().contains("SWDATA_OBS_DEN_SWARMB");})
                .collect(Collectors.toList());
        //不同卫星
            List<String> hour = resultSaves.stream().map(a -> {
                String startTime = a.getStartTime();
                String index = "";
                String[] split = a.getParams().split("----");
                for (String s : split) {
                    if (s.startsWith("hour")) {
                        index = s.split("::::")[1];
                    }
                }
                return startTime + ":" + index;
            }).collect(Collectors.toList());

        HashSet<String> strings = new HashSet<>();
        for (String s : timeSet) {

                if (!hour.contains(s)) {
                    System.out.println("不存在:    " + s);
                    strings.add(s.substring(0,10));
                }
            }
        List<String> collect = strings.stream().sorted().collect(Collectors.toList());
        System.out.println(JSONUtil.toJsonStr(collect));
        System.exit(1);
    }

    @Override
    public void resultSave(Object param, ResultSave entity) {
        String paramStr = "";
        if (param instanceof String) {
            paramStr = (String) param;
        }else {
            JSONConfig jsonConfig = JSONConfig.create().setDateFormat("yyyy-MM-dd HH:mm:ss");
            String jsonStr = JSONUtil.toJsonStr(param,jsonConfig);
            JSONObject params = JSONUtil.parseObj(jsonStr,jsonConfig);
            paramStr = params.entrySet().stream()
                    .filter(o -> {if (o.getValue() instanceof String){return StringUtils.hasLength((String) o.getValue());} else {return o.getValue() != null;}})
                    .map(o ->  o.getKey() + Constants.ENTRY_D + o.getValue())
                    .sorted()
                    .collect(Collectors.joining(Constants.FIELD_D));
        }
        entity.setParams(paramStr);
        //update 增加文件名
        List<String> fileNameList = JSONUtil.parseArray(entity.getResultData()).toList(String.class)
                .stream()
                .filter(file -> {return file != null;})
                .map(filePath -> filePath.substring(filePath.lastIndexOf("/") + 1))
                .collect(Collectors.toList());
        String fileNames = JSONUtil.toJsonStr(fileNameList);
        entity.setFileNames(fileNames);

        LambdaUpdateWrapper<ResultSave> uw = new LambdaUpdateWrapper();
        uw.eq(ResultSave::getModule, entity.getModule());
        uw.eq(ResultSave::getSubmodule, entity.getSubmodule());
        uw.eq(StrUtil.isNotBlank(entity.getStartTime()), ResultSave::getStartTime, entity.getStartTime());
        uw.eq(StrUtil.isNotBlank(entity.getEndTime()), ResultSave::getEndTime, entity.getEndTime());
        uw.eq(ResultSave::getParams, paramStr);

        this.saveOrUpdate(entity,uw);

    }
}
